package com.by;


import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Productor2 {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1.创建连接工厂   创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("erp");//虚拟主机 默认是 /
        factory.setPort(5672); //连接端口号
        factory.setHost("192.168.221.131");//服务器地址 默认是localhost
        factory.setUsername("admin");
        factory.setPassword("1357");

        //2.创建连接  资源自动释放（连接、信道 关闭）
        try (Connection conn = factory.newConnection(); Channel channel = conn.createChannel()) {

            //3. 声明延迟队列
            String q1="lazyQueue";
            channel.queueDeclare(q1,false,false,false,null);


            //4.声明延迟交换器
            String ex="lazyEx";
            //指定延迟交换器
            Map<String,Object> arg=new HashMap<>();
            arg.put("x-delayed-type","direct");//交换器类型为路由键
            channel.exchangeDeclare(ex,"x-delayed-message",true,false,arg);
            //5.绑定延迟交换器与队列
            channel.queueBind(q1,ex,"lazy");


            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put("x-delay", 5000);//代表延迟的时间
            AMQP.BasicProperties.Builder props = new AMQP.BasicProperties.Builder().headers(headers);
            channel.basicPublish(ex, "lazy", props.build(), "dd20220001".getBytes());

            System.out.println("消息发布完毕，服务器已收到");

        }


    }
}